package ksj;

import javax.servlet.Filter;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.CommonsRequestLoggingFilter;

import ksj.common.tasks.ScheduleCheckBeat;

//@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@SpringBootApplication(scanBasePackages = { "ksj.*" })
public class ConfigServiceApplication {

	@Bean
	public Filter loggingFilter() {
		CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
		filter.setIncludeQueryString(true);
		filter.setIncludePayload(true);
		filter.setMaxPayloadLength(5120);
		filter.setIncludeClientInfo(true);
		return filter;
	}

	/*
	 * @Bean public FilterRegistrationBean loggingFilterRegistration() {
	 * FilterRegistrationBean registration = new
	 * FilterRegistrationBean(loggingFilter());
	 * registration.addUrlPatterns("/*"); return registration; }
	 */

	public static void main(String[] args) {
		try {
			SpringApplication.run(ConfigServiceApplication.class, args);

			JobDetail job = JobBuilder.newJob(ScheduleCheckBeat.class).withIdentity("dummyJobName", "group1").build();

			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("dummyTriggerName", "group1")
					.withSchedule(
							SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatMinutelyForever(1))
					.build();

			Scheduler scheduler = new StdSchedulerFactory().getScheduler();

			scheduler.start();
			try {
				scheduler.scheduleJob(job, trigger);
			} catch (Exception e) {
				scheduler.deleteJob(job.getKey());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
